package dao

import (
	"fmt"
	__ "user-srv/basic/proto"
	"user-srv/handler/models"
)

// 会员权益添加
func MemberBenefitAdd(in *__.MemberBenefitAddReq) (*models.MemberBenefit, error) {
	var memberBenefit models.MemberBenefit
	memberBenefit.Name = in.Name
	memberBenefit.Desc = in.Desc
	memberBenefit.Types = uint8(in.Types)
	err := memberBenefit.MemberBenefitAdd()
	if err != nil {
		return nil, fmt.Errorf("会员权益添加失败")
	}
	return &memberBenefit, nil
}

// 会员权益查询
func MemberBenefitFind(in *__.MemberBenefitFindReq) ([]*__.MemberBenefitFind, error) {
	var memberBenefit models.MemberBenefit
	var memberBenefitFind []*__.MemberBenefitFind

	find, err := memberBenefit.MemberBenefitFind()
	if err != nil {
		return nil, fmt.Errorf("会员权益查询失败")
	}
	for _, benefit := range find {
		memberBenefitFind = append(memberBenefitFind, &__.MemberBenefitFind{
			Id:    benefit.Id,
			Name:  benefit.Name,
			Desc:  benefit.Desc,
			Types: uint64(benefit.Types),
		})
	}
	return memberBenefitFind, nil
}

// 会员权益详情
func MemberBenefitDetail(in *__.MemberBenefitDetailReq) (*models.MemberBenefit, error) {
	var memberBenefit models.MemberBenefit
	err := memberBenefit.GetMemberBenefitInfoById(int(in.Id))
	if err != nil {
		return nil, fmt.Errorf("会员权益详情查询失败")
	}

	return &memberBenefit, nil
}
